﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using OpenExpressApp.Editors;
using System.Collections;
using System.ComponentModel;
using Csla.Core;
using System.Windows.Controls;

namespace OpenExpressApp.Module.WPF
{
    /// <summary>
    /// 某对象的详细显示视图
    /// </summary>
    public class DetailObjectView : WPFObjectView, IDetailObjectView
    {
        private ListObjectView _listView;

        public DetailObjectView(Type boType) : base(boType) { }

        /// <summary>
        /// 可能会有一个列表对应这个详细视图
        /// </summary>
        public ListObjectView ListView
        {
            get
            {
                return this._listView;
            }
            set
            {
                this._listView = value;
            }
        }
        /// <summary>
        /// DetailObjectView的CurrentObject就是Data。
        /// </summary>
        public override object CurrentObject
        {
            get
            {
                return this.Data;
            }
            set
            {
                this.Data = value;
            }
        }

        public override void RefreshCurrentObject()
        {
            this.SetCurrentObject(this.CurrentObject);
            //this.OnCurrentObjectChanged(EventArgs.Empty);
            //this.Control.DataContext = this.CurrentObject;//赋值后TextBox控件的IsReadOnly会重置，不知原因？？
        }

        /// <summary>
        /// 使用AutoUI生成控件
        /// </summary>
        /// <returns></returns>
        protected override object CreateControl()
        {
            return AutoUI.UIFactory.CreateDetailView(this, 2);
        }
        protected override void OnDataChanging()
        {
            var currentModel = this.Data as INotifyPropertyChanged;
            if (currentModel != null)
            {
                currentModel.PropertyChanged -= CurrentObject_PropertyChanged;
            }

            base.OnDataChanging();
        }
        /// <summary>
        /// 设置时，刷新子视图
        /// </summary>
        protected override void OnDataChanged()
        {
            object currentObject = null;

            IList list = this.Data as IList;
            if (list == null)
            {
                currentObject = this.Data;
            }
            else if (list.Count > 0)
            {
                currentObject = list[0];
            }

            if (currentObject != null)
            {
                this.SetCurrentObject(currentObject);

                var currentModel = currentObject as INotifyPropertyChanged;
                if (currentModel != null)
                {
                    currentModel.PropertyChanged -= CurrentObject_PropertyChanged;
                    currentModel.PropertyChanged += CurrentObject_PropertyChanged;
                }
            }

            base.OnDataChanged();
        }
        protected virtual void OnCurrentObjectPropertyChanged(PropertyChangedEventArgs e)
        {
            //当前属性是明细时,如果更改了属性值,则需要自动刷新细表
            //BusinessObjectsPropertyInfo info = (ApplicationModel.GetBusinessObjectInfo(BOType).BOsInfoByName(e.PropertyName);
            //if (null != info)
            //{
            foreach (var view in ChildViews)
            {
                if (view.PropertyName == e.PropertyName)
                {
                    view.Data = CurrentObject.GetPropertyValue(view.PropertyName);
                }
            }
            //}
        }

        private void SetCurrentObject(object currentObject)
        {
            this.Control.DataContext = currentObject;

            this.OnCurrentObjectChanged();
        }
        /// <summary>
        /// 属性更改时，更新子视图的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.OnCurrentObjectPropertyChanged(e);
        }

        #region IDetailObjectView Members

        IListObjectView IDetailObjectView.ListView
        {
            get
            {
                return this.ListView;
            }
        }

        #endregion
    }
}